package middleware

import (
	"time"

	"github.com/gin-gonic/gin"
	"goserver/internal/metrics"
)

func Metrics() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()

		// 处理请求
		c.Next()

		// 记录请求延迟
		duration := time.Since(start).Seconds()
		metrics.APILatency.WithLabelValues(
			c.Request.Method,
			c.FullPath(),
		).Observe(duration)

		// 记录请求计数
		metrics.APIRequestCounter.WithLabelValues(
			c.Request.Method,
			c.FullPath(),
			c.Writer.Status(),
		).Inc()
	}
} 